<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
        <span aria-hidden="true">&times;</span></button>
    <h4 class="modal-title">角色信息</h4>
</div>
<div class="modal-body">
    <div class="form-horizontal row">
        <form id="form-data">
            <div id="div_error" class="col-sm-12"></div>
            <input type="hidden" id="id" name="id" value="${data.id}">
            <div class="form-group col-sm-6">
                <label class="control-label col-sm-4"><s>*</s>名称：</label>
                <div class="col-sm-7">
                    <input type="text" class="form-control" name="name" value="${data.name}">
                </div>
            </div>

            <div class="form-group col-sm-6">
                <label class="col-sm-4 control-label"><s>*</s>状态：</label>
                <div class="col-sm-7">
                    <select class="form-control" name="live">
                        <option value="">&nbsp;</option>
                        <option
                                <c:if test="${data.live==0}">selected</c:if> value="0">停用
                        </option>
                        <option
                                <c:if test="${data.live==1}">selected</c:if> value="1">启用
                        </option>
                    </select>
                </div>
            </div>

            <div class="form-group col-sm-6">
                <label class="control-label col-sm-4"><s>*</s>权限：</label>
                <div class="col-sm-7">
                        <ul id="treeDemo1" class="ztree form-control-free"></ul>
                </div>
            </div>


            <div class="form-group col-sm-6">
                <label class="control-label col-sm-4">备注：</label>

                <div class="col-sm-7">
                        <textarea class="form-control " name="remark" rows="4">${data.remark}</textarea>
                </div>
            </div>


            <div class="form-group col-sm-6">
                <div class="col-sm-7 col-sm-offset-4">
                    <button type="submit"
                            class="btn btn-default btn-success"><span
                            class="glyphicon glyphicon-ok"></span>&nbsp;确&nbsp;定&nbsp;
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>


    $(function () {
        var  setting = {
            check: {
                enable: true,
                checkboxType: {"Y": "", "N": ""}
            },
            view: {
                dblClickExpand: false
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            async: {
                enable: true,
                url:$("#id").val() == ''?"upms/permission/selectAllActivePermission":"upms/permission/selectAllActivePermissionByRoleId",
//                autoParam: ["id"],
                otherParam:{"roleId":$("#id").val()},
//                dataFilter: filter
            }
        };
        $.fn.zTree.init($("#treeDemo1"), setting);
    });


    //
    //    function beforeClick(treeId, treeNode) {
    //        var zTree = $.fn.zTree.getZTreeObj("treeDemo");
    //        zTree.checkNode(treeNode, !treeNode.checked, null, true);
    //        return false;
    //    }
    //
    //    function onCheck(e, treeId, treeNode) {
    //        var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
    //                nodes = zTree.getCheckedNodes(true),
    //                v = "";
    //        for (var i = 0, l = nodes.length; i < l; i++) {
    //            v += nodes[i].name + ",";
    //        }
    //        if (v.length > 0) v = v.substring(0, v.length - 1);
    //        var cityObj = $("#citySel");
    //        cityObj.attr("value", v);
    //    }
    //
    //    function showMenu() {
    //        var cityObj = $("#citySel");
    //        var cityOffset = $("#citySel").offset();
    //        $("#menuContent").css({
    ////            left: cityOffset.left + "px",
    ////            top: cityOffset.top + cityObj.outerHeight() + "px"
    //        }).slideDown("fast");
    //        $("body").bind("mousedown", onBodyDown);
    //    }
    //    function hideMenu() {
    //        $("#menuContent").fadeOut("fast");
    //        $("body").unbind("mousedown", onBodyDown);
    //    }
    //    function onBodyDown(event) {
    //        if (!(event.target.id == "menuBtn" || event.target.id == "citySel" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length > 0)) {
    //            hideMenu();
    //        }
    //    }
    //    $(document).ready(function () {
    //        $.fn.zTree.init($("#treeDemo"), setting, zNodes);
    //    });
</script>

<script>
    <%--bootstrapValidator--%>
    var $form = $("#form-data");
    $form.bootstrapValidator({
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            name: {
                validators: {
                    notEmpty: {
                        message: '不能为空！'
                    }
                }
            }, live: {
                validators: {
                    notEmpty: {
                        message: '不能为空！'
                    }
                }
            }, permission: {
                validators: {
                    notEmpty: {
                        message: '不能为空！'
                    }
                }
            }
        }
    }).on('success.form.bv', function (e) {
        e.preventDefault();

        var treeObj = $.fn.zTree.getZTreeObj("treeDemo1");
        var nodes = treeObj.getCheckedNodes(true);
        var ids = new Array();
        for (i = 0; i < nodes.length; i++) {
            ids[i] = nodes[i].id;
        }

         if (ids.length==0){
            $("#div_error").html( $.sysPlugin.createAlert("权限不能为空"));
             return;
         }


         var data= $(e.target).serializeObject();
        data['permission']=ids;

        var url, operateType;
        if ($("#id").val() == '') {
            url = "upms/role/insert";
            operateType = "insert";
        } else {
            url = "upms/role/update";
            operateType = "update";
        }
        $.sysPlugin.superAjax({
            data: data,
            url: url,
            operateType: operateType,
            showSuccessAlert: true,
            showWarnAlert: false,
            successCall: function (data) {
                $.sysPlugin.successCallBack(data, {
                    modal: $("#modal_ajax"),
                    table: $("#table_data")
                })
            },
            errorCall: function (data) {
                $.sysPlugin.errorCallBack(data, {
                    errorAddress: $("#div_error"),
                    form: $form
                })
            }
        });
    });
    $("select").select2({
        minimumResultsForSearch: Infinity,
        width: '100%'
    });
</script>
